﻿namespace Example.Runtime.InteropServices
{
    using System;
    using System.Runtime.InteropServices;

    /// <summary>Defines entry points to the GDI32 API.</summary>
    internal static class NativeMethods
    {
        /// <summary>Deletes the specified device context (DC).</summary>
        /// <param name="hdc">Handle to the device context.</param>
        /// <returns>
        /// Returns <see langword="true"/> if the function succeeds; otherwise <see langword="false"/>.
        /// </returns>
        /// <seealso href="http://msdn2.microsoft.com/library/ms533229(VS.85).aspx">MSDN</seealso>
        /// <seealso href="http://pinvoke.net/default.aspx/gdi32.DeleteDC">pinvoke.net</seealso>
        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DeleteDC(IntPtr hdc);

        /// <summary>
        /// Performs a bit-block transfer of the color data corresponding to a rectangle of pixels
        /// from the specified source device context into a destination device context.
        /// </summary>
        /// <param name="hdcDest">Handle to the destination device context.</param>
        /// <param name="nXDest">Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle.</param>
        /// <param name="nYDest">Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle.</param>
        /// <param name="nWidth">Specifies the width, in logical units, of the source and destination rectangles.</param>
        /// <param name="nHeight">Specifies the height, in logical units, of the source and the destination rectangles.</param>
        /// <param name="hObjSource">Handle to the source device context.</param>
        /// <param name="nXSrc">Specifies the x-coordinate, in logical units, of the upper-left corner of the source rectangle.</param>
        /// <param name="nYSrc">Specifies the y-coordinate, in logical units, of the upper-left corner of the source rectangle.</param>
        /// <param name="dwRop">
        /// Specifies a raster-operation code.
        /// These codes define how the color data for the source rectangle is to be combined
        /// with the color data for the destination rectangle to achieve the final color.
        /// </param>
        /// <returns>
        /// Returns <see langword="true"/> if the function succeeds; otherwise <see langword="false"/>.
        /// </returns>
        /// <seealso href="http://msdn2.microsoft.com/library/ms532278(VS.85).aspx">MSDN</seealso>
        /// <seealso href="http://pinvoke.net/default.aspx/gdi32.BitBlt">pinvoke.net</seealso>
        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);

        /// <summary>
        /// Creates a bitmap compatible with the device that is associated with the specified device context.
        /// </summary>
        /// <param name="hdc">Handle to a device context.</param>
        /// <param name="nWidth">Specifies the bitmap width, in pixels.</param>
        /// <param name="nHeight">Specifies the bitmap height, in pixels.</param>
        /// <returns>
        /// <para>If the function succeeds, the return value is a handle to the compatible bitmap (DDB).</para>
        /// <para>If the function fails, the return value is <see langword="null"/>.</para>
        /// </returns>
        /// <seealso href="http://msdn2.microsoft.com/library/ms532287(VS.85).aspx">MSDN</seealso>
        /// <seealso href="http://pinvoke.net/default.aspx/gdi32.CreateCompatibleBitmap">pinvoke.net</seealso>
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

        /// <summary>
        /// Creates a memory device context (DC) compatible with the specified device.
        /// </summary>
        /// <param name="hdc">
        /// Handle to an existing DC.
        /// If this handle is <see langword="null"/>,
        /// the function creates a memory DC compatible with the application's current screen.
        /// </param>
        /// <returns>
        /// <para>If the function succeeds, the return value is the handle to a memory DC.</para>
        /// <para>If the function fails, the return value is <see langword="null"/>.</para>
        /// </returns>
        /// <seealso href="http://msdn2.microsoft.com/library/ms533239(VS.85).aspx">MSDN</seealso>
        /// <seealso href="http://pinvoke.net/default.aspx/gdi32.CreateCompatibleDC">pinvoke.net</seealso>
        [DllImport("gdi32.dll", SetLastError = true)]
        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        /// <summary>
        /// Selects an object into the specified device context (DC).
        /// The new object replaces the previous object of the same type.
        /// </summary>
        /// <param name="hdc">Handle to the DC.</param>
        /// <param name="hgdiobj">
        /// Handle to the object to be selected.
        /// The specified object must have been created by using one of the following functions.
        /// </param>
        /// <returns>
        /// If the selected object is not a region and the function succeeds, the return value is a handle to the object being replaced.
        /// If the selected object is a region and the function succeeds, the return value is one of the following values.
        /// </returns>
        /// <seealso href="http://msdn2.microsoft.com/library/ms533272(VS.85).aspx">MSDN</seealso>
        /// <seealso href="http://pinvoke.net/default.aspx/gdi32.SelectObject">pinvoke.net</seealso>
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
    }
}